package com.moxiao.yfjh.module.activity.controller.admin.packageInfo;

import com.moxiao.yfjh.module.activity.common.GenActiveNoCommonUtil;
import com.moxiao.yfjh.module.activity.controller.admin.packageProduct.vo.PackageProductCreateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.packageType.vo.PackageTypeExportReqVO;
import com.moxiao.yfjh.module.activity.dal.dataobject.packageType.PackageTypeDO;
import com.moxiao.yfjh.module.activity.service.merchantInfo.MerchantInfoService;
import com.moxiao.yfjh.module.activity.service.packageProduct.PackageProductService;
import com.moxiao.yfjh.module.activity.service.packageType.PackageTypeService;
import com.moxiao.yfjh.module.activity.service.productInfo.ProductInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;

import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;

import com.moxiao.yfjh.module.activity.controller.admin.packageInfo.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.packageInfo.PackageInfoDO;
import com.moxiao.yfjh.module.activity.convert.packageInfo.PackageInfoConvert;
import com.moxiao.yfjh.module.activity.service.packageInfo.PackageInfoService;

@Tag(name = "管理后台 - 相亲套餐信息")
@RestController
@RequestMapping("/activity/package-info")
@Validated
public class PackageInfoController {

    @Resource
    private PackageInfoService packageInfoService;

    @Resource
    private GenActiveNoCommonUtil genActiveNoCommonUtil;

    @Resource
    private PackageTypeService packageTypeService;
    @Resource
    private PackageProductService packageProductService;
    @Resource
    private ProductInfoService productInfoService;

    @Resource
    private MerchantInfoService merchantInfoService;



    @PostMapping("/create")
    @Operation(summary = "创建相亲套餐信息")
    @PreAuthorize("@ss.hasPermission('activity:package-info:create')")
    public CommonResult<Long> createPackageInfo(@Valid @RequestBody PackageInfoCreateReqVO createReqVO) {
        genActiveNoCommonUtil.addPackageInfoNo(createReqVO);
        Long packageInfo = packageInfoService.createPackageInfo(createReqVO);
        if (Objects.nonNull(packageInfo)){
            List<PackageProductCreateReqVO> packageProductCreateReqVOS = createReqVO.getReqVO();
            packageProductCreateReqVOS.forEach(packageProductCreateReqVO -> {
                packageProductCreateReqVO.setPackageId(packageInfo);
            });
            packageProductService.createPackageProduct(packageProductCreateReqVOS);
        }
        return success(packageInfo);
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲套餐信息")
    @PreAuthorize("@ss.hasPermission('activity:package-info:update')")
    public CommonResult<Boolean> updatePackageInfo(@Valid @RequestBody PackageInfoUpdateReqVO updateReqVO) {
        packageInfoService.updatePackageInfo(updateReqVO);
        packageProductService.updatePackageProduct(updateReqVO.getReqVO());
        return success(true);
    }

    @GetMapping("/getMerchantTypeList")
    @Operation(summary = "获得相亲商家类型列表")
    public CommonResult<List<PackageTypeDO>> getPackageTypeList() {
        return success(packageTypeService.getPackageTypeList(new PackageTypeExportReqVO()));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲套餐信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:package-info:delete')")
    public CommonResult<Boolean> deletePackageInfo(@RequestParam("id") Long id) {
        packageInfoService.deletePackageInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲套餐信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:package-info:query')")
    public CommonResult<PackageInfoRespVO> getPackageInfo(@RequestParam("id") Long id) {
        PackageInfoDO packageInfo = packageInfoService.getPackageInfo(id);
        return success(PackageInfoConvert.INSTANCE.convert(packageInfo));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲套餐信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:package-info:query')")
    public CommonResult<List<PackageInfoRespVO>> getPackageInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<PackageInfoDO> list = packageInfoService.getPackageInfoList(ids);
        return success(PackageInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲套餐信息分页")
    @PreAuthorize("@ss.hasPermission('activity:package-info:query')")
    public CommonResult<PageResult<PackageInfoRespVO>> getPackageInfoPage(@Valid PackageInfoPageReqVO pageVO) {
        PageResult<PackageInfoDO> pageResult = packageInfoService.getPackageInfoPage(pageVO);
        return success(PackageInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲套餐信息 Excel")
    @PreAuthorize("@ss.hasPermission('activity:package-info:export')")
    @OperateLog(type = EXPORT)
    public void exportPackageInfoExcel(@Valid PackageInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PackageInfoDO> list = packageInfoService.getPackageInfoList(exportReqVO);
        // 导出 Excel
        List<PackageInfoExcelVO> datas = PackageInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲套餐信息.xls", "数据", PackageInfoExcelVO.class, datas);
    }

}
